package generator.controller;

import generator.StuVo.StuVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/redis")
public class RedisController {

    private final RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取redis中的值
     *
     * @param keyName
     * @return
     */
    @GetMapping("/get")
    public Object get(String keyName) {
        return redisTemplate.opsForValue().get(keyName);
    }

    /**
     * 设置redis中的值
     *
     * @param keyName
     * @param value
     */
    @GetMapping("/set")
    public void set(String keyName, String value) {
        redisTemplate.opsForValue().set(keyName, value);
        Object o = redisTemplate.opsForValue().get(keyName);
        log.info("redis set key:{}, value:{}", keyName, o);
    }

    /**
     * 删除redis中的值
     * @param keyName
     */
    @DeleteMapping("/del")
    public void del(String keyName) {
        redisTemplate.delete(keyName);
        log.info("redis del key:{}", keyName);
    }

    /**
     * 更新redis中的值
     * @param keyName
     * @param value
     */
    @GetMapping ("/update")
    public void update(String keyName, String value) {
        redisTemplate.opsForValue().set(keyName, value);
        log.info("redis update key:{}, value:{}", keyName, value);
    }

}
